package cn.itcast.nems.base.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.nems.base.dto.DiscountsDTO;
import cn.itcast.nems.discount.dto.DiscountSummaryDTO;
import cn.itcast.nems.discounts.dao.dataobject.EmsDiscountsDO;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.studentrecord.service.DiscountCalculator;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class DiscountUtil {
    private DiscountUtil() {}

    public static DiscountsDTO transferDiscountDTO(EmsDiscountsDO discountsDO, BigDecimal tuitionFee, Integer remainMoney) {
        DiscountsDTO discountsDTO = CustomBeanUtil.copyProperties(discountsDO, new DiscountsDTO());
        discountsDTO.setDiscountsMoney(Objects.nonNull(discountsDO.getDiscountsMoney()) ?
                discountsDO.getDiscountsMoney().setScale(2, RoundingMode.HALF_UP).toString() : null);
        discountsDTO.setDiscountsRate(Objects.nonNull(discountsDO.getDiscountsRate()) ?
                BigDecimal.valueOf(discountsDO.getDiscountsRate()).setScale(2, RoundingMode.HALF_UP).toString() : null);
        discountsDTO.setClazzBeginDate(discountsDO.getClazzBeginDate());
        discountsDTO.setShowDiscountMoney(discountsDTO.getDiscountsMoney());
        if (discountsDO.getDiscountsRate() > 0) {
            discountsDTO.setShowDiscountMoney(tuitionFee.multiply(BigDecimal.valueOf(discountsDO.getDiscountsRate())).setScale(2, RoundingMode.HALF_UP).toString());
        }
        if (StringUtils.hasText(discountsDTO.getShowDiscountMoney()) ) {
            BigDecimal remainBigdecimal  = Objects.nonNull(remainMoney) ? BigDecimal.valueOf(remainMoney).divide(BigDecimal.valueOf(100),4,RoundingMode.HALF_UP) : tuitionFee ;
            if (BigDecimal.valueOf(Double.parseDouble(discountsDTO.getShowDiscountMoney())).compareTo(remainBigdecimal) > 0) {
                discountsDTO.setShowDiscountMoney(remainBigdecimal.setScale(2, RoundingMode.HALF_UP).toString());
            }
        }
        return discountsDTO;
    }
    public static List<DiscountsDTO> getDiscountList (DiscountCalculator discountCalculator , List<DiscountSummaryDTO> summaryList) {
        if(CollectionUtil.isNotEmpty(summaryList)) {
            Map<Integer, Integer> map=  discountCalculator.getDiscountAmounts() ;
            List<DiscountsDTO> discounts =  new ArrayList<>();
            for (int i = 0; i < summaryList.size(); i++) {
                DiscountSummaryDTO summaryDTO = summaryList.get(i);
                Integer amount = map.get(summaryDTO.getId()) ;
                DiscountsDTO discountsDTO =new DiscountsDTO() ;
                discountsDTO.setDiscountsMoney(Objects.nonNull(amount) ?
                        BigDecimal.valueOf(amount).divide(BigDecimal.valueOf(100) ,2,RoundingMode.HALF_UP).toString():null);
                discountsDTO.setId(summaryDTO.getId());
                discountsDTO.setDiscountsName(summaryDTO.getName());
                discounts.add(discountsDTO) ;
            }
            return discounts ;
        }
        return  null;
    }
}
